package com.z_talk.client.view;

import com.sun.xml.internal.ws.util.StringUtils;
import com.z_talk.client.core.ClientSender;
import com.z_talk.core.enums.RequestEnum;
import com.z_talk.core.message.RequestMessage;
import com.z_talk.core.model.UserInfo;
import com.z_talk.core.util.TipsView;
import io.netty.util.internal.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class RegisterView {
    private JPanel registerPanel;
    private JButton confirmBut;
    private JButton cancelBut;
    private JTextField nicknameText;
    private JPasswordField confirmPassword;
    private JTextField ageText;
    private JPasswordField password;
    private JRadioButton female;
    private JRadioButton male;

    private static ClientSender clientSender;

    public RegisterView() {
        this.clientSender = ClientSender.getInstance();
        init();
    }

    private void init(){
        JFrame jFrame = new JFrame("注册");
        jFrame.setContentPane(this.registerPanel);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setSize(500,350);
        int x = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int y = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        jFrame.setLocation((x - jFrame.getWidth()) / 2, (y-jFrame.getHeight())/ 2);
        jFrame.setResizable(false);

        nicknameText.requestFocus();

        // 添加事件监听
        confirmBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nickname = nicknameText.getText();
                String password2 = new String(confirmPassword.getPassword());
                String age= ageText.getText();
                String password1 = new String(password.getPassword());

                if (password1==null||password1==null||"".equals(password1)||"".equals(password1)){
                    TipsView.warning("必填项未填","警告");
                    return;
                }

                if (!password2.equals(password1)){
                    System.out.println("密码输入不正确");
                }
                String sex = female.isSelected()?"女":"男";
                UserInfo userInfo = new UserInfo(nickname,password2,age,sex);
                try {
                    RequestMessage message = new RequestMessage();
                    message.setMessageType(RequestEnum.REGISTER_REQUEST);
                    Map<String,Object> attribute = new HashMap<>();
                    attribute.put(RequestEnum.REGISTER_REQUEST.getAttr(),userInfo);
                    message.setAttribute(attribute);
                    clientSender.sendMsg(message);
                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                }
                jFrame.dispose();
            }
        });
        cancelBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.dispose();
            }
        });
    }
}
